{
 "cells": [
  {
   "metadata": {},
   "cell_type": "code",
   "outputs": [],
   "execution_count": null,
   "source": [
    "import warnings\n",
    "warnings.filterwarnings(\"ignore\")\n",
    "import numpy as np\n",
    "import pandas as pd\n",
    "import matplotlib.pyplot as plt\n",
    "import os\n",
    "os.chdir('./')"
   ],
   "id": "670c4679b4c0e1a7"
  },
  {
   "metadata": {},
   "cell_type": "code",
   "outputs": [],
   "execution_count": null,
   "source": [
    "import plotly as py\n",
    "import plotly.graph_objs as go\n",
    "py.offline.init_notebook_mode() # 初始化\n",
    "pyplot = py.offline.iplot   # 画图"
   ],
   "id": "41c4c5c07fb26cc"
  },
  {
   "metadata": {},
   "cell_type": "code",
   "outputs": [],
   "execution_count": null,
   "source": [
    "df = pd.read_csv('data.csv', encoding='ISO-8859-1', dtype={'CustomerId':str})\n",
    "df.apply(lambda x: sum(x.isnull())/len(x),axis=0)    # 查看数据中每一列的缺失值比例\n",
    "df.drop(['Description'],axis=1,inplace=True)    # 删除Description列\n",
    "df.dropna(inplace=True) # 如果用户ID为空，则删除该行"
   ],
   "id": "31d91d24444e9e7b"
  },
  {
   "metadata": {},
   "cell_type": "code",
   "outputs": [],
   "execution_count": null,
   "source": "df['amount'] = df['Quantity']*df['UnitPrice']   # 计算合计购买金额",
   "id": "df0603dc1bdc950d"
  },
  {
   "metadata": {},
   "cell_type": "code",
   "outputs": [],
   "execution_count": null,
   "source": [
    "# 订单日期划分\n",
    "df['date'] = [x.split(' ')[0] for x in df['InvoiceDate']]\n",
    "df['time'] = [x.split(' ')[1] for x in df['InvoiceDate']]\n",
    "df.drop(['InvoiceDate'], axis=1, inplace=True)\n",
    "df['year'] = [x.split('/')[2] for x in df['date']]\n",
    "df['month'] = [x.split('/')[0] for x in df['date']]\n",
    "df['day'] = [x.split('/')[1] for x in df['date']]"
   ],
   "id": "f9b7262482ba03ad"
  },
  {
   "metadata": {},
   "cell_type": "code",
   "outputs": [],
   "execution_count": null,
   "source": [
    "# 删除重复数据\n",
    "df.drop_duplicates()"
   ],
   "id": "fee8df99b4a4ed47"
  },
  {
   "metadata": {},
   "cell_type": "code",
   "outputs": [],
   "execution_count": null,
   "source": [
    "# 计算异常数据比例\n",
    "df1 = df.loc[df['Quantity'] <= 0]\n",
    "print('异常数据比例：%.2f%%' % df1.shape[0]/df.shape[0])"
   ],
   "id": "1f6650827aace443"
  }
 ],
 "metadata": {},
 "nbformat": 4,
 "nbformat_minor": 5
}
